package com.wangzhen.spring.inject;

import com.wangzhen.spring.inject.annotation.UserGroup;
import com.wangzhen.spring.ioc.dependcy.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

import java.util.Collection;

/**
 * 限定注入 演示示例
 * 背景：user和superUser都是User类型的bean
 *  @Autowired User user 注入的话，哪一个指定是primary，哪一个就会被注入；
 * 如果指定@Qualifier注解的话，就会按照名称注入
 */
public class QualifierExtensionInjectDemo {

    // xml文件中配置的User，primary=true
    // 存在同一类型的多个bean对象，谁指定了Primary谁就会被注入，如果都没有指定，就会报NoUniqueBeanDefinitionException
    @Autowired
    private User user;

    // xml文件中配置的User，primary ！= true
    // Autowired+Qualifier 组合的效果是：按照Qualifier指定的名称进行注入
    @Autowired
    @Qualifier("superUser")
    private User superUser;

    // 期待输出所有的User  xml中的user+superUser
    // 以及@Bean配置的qualifierUser1/qualifierUser2
    // 以及继承了Qualifier注解的userGroup1/userGroup2

    // 但是实际上只会输出xml中的user+superUser
    @Autowired
    private Collection<User> allUser;

    // 只会输出@Bean+@Qualifier配置的qulifierUser1+qualifierUser2, 以及继承了@Qualifier的userGroup1/userGroup2
    @Autowired
    @Qualifier
    private Collection<User> allQualifierUsers;

    // 只会输出@Bean+@UserGroup配置的userGroup1+userGroup2
    @Autowired
    @UserGroup
    private Collection<User> allUserGroupQualifierUsers;

    @Bean
    @Qualifier
    public User qualifierUser1() {
        User user = new User();
        user.setName("qualifierUser1");
        return user;
    }

    @Bean
    @Qualifier
    public User qualifierUser2() {
        User user = new User();
        user.setName("qualifierUser2");
        return user;
    }

    @Bean
    @UserGroup
    public User userGroup1() {
        User user = new User();
        user.setName("userGroup1");
        return user;
    }

    @Bean
    @UserGroup
    public User userGroup2() {
        User user = new User();
        user.setName("userGroup2");
        return user;
    }

     public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(QualifierExtensionInjectDemo.class);
        // 使用注解方式驱动的AnnotationConfigApplicationContext，也可以加载xml配置
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(applicationContext);
        reader.loadBeanDefinitions("classpath:/META-INF/dependcy-lookup-context.xml");
        // 启动应用上下文
        applicationContext.refresh();
        // 获取UserHolder
        QualifierExtensionInjectDemo demo = applicationContext.getBean(QualifierExtensionInjectDemo.class);
        System.out.println("demo.user ="+demo.user);
        System.out.println("demo.superUser ="+demo.superUser);
        System.out.println("demo.allUser ="+demo.allUser);
        System.out.println("demo.allQualifierUsers ="+demo.allQualifierUsers);
        System.out.println("demo.allUserGroupQualifierUsers ="+demo.allUserGroupQualifierUsers);
        // 关闭应用上下文
        applicationContext.close();
    }

}
